package com.linkdood.app.web.rest;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linkdood.app.constants.SystemNoticeConstants;
import com.linkdood.app.domain.SystemNotice;
import com.linkdood.app.service.SystemNoticeService;
import com.linkdood.app.utils.PaginationUtil;
import com.linkdood.app.utils.ResourceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统消息接口
 * @author YunJ
 */
@Api(value = "系统消息接口")
@Slf4j
@RestController
@RequestMapping("/api/system/notices")
public class SystemNoticeResource {

    private final SystemNoticeService systemNoticeService;

    public SystemNoticeResource(SystemNoticeService systemNoticeService) {
        this.systemNoticeService = systemNoticeService;
    }

    @ApiOperation(value = "获取消息列表")
    @GetMapping
    public ResponseEntity<List<SystemNotice>> listNotices(@RequestParam(required = false) String status,
                                         Pageable pageable) {
        Page<SystemNotice> noticePage = systemNoticeService.pageSystemNoticesByStatus(status, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(noticePage);
        return new ResponseEntity<>(noticePage.getRecords(), headers, HttpStatus.OK);
    }

    @ApiOperation(value = "删除消息")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, String>> deleteNotice(@PathVariable String id) {
        systemNoticeService.removeById(id);
        systemNoticeService.sendMessage();
        return ResponseEntity.ok(ResourceUtil.success());
    }

    @ApiOperation(value = "批量删除消息")
    @DeleteMapping
    public ResponseEntity<Map<String, String>> deleteNotices(@RequestBody List<String> ids) {
        systemNoticeService.removeByIds(ids);
        systemNoticeService.sendMessage();
        return ResponseEntity.ok(ResourceUtil.success());
    }

    @ApiOperation(value = "标记已读消息")
    @PostMapping
    public ResponseEntity<Map<String, String>> readNotices(@RequestBody List<String> ids) {
        List<SystemNotice> result = new ArrayList<>();
        List<SystemNotice> systemNotices = systemNoticeService.listSystemNoticeByIds(ids);
        for (SystemNotice systemNotice : systemNotices) {
            systemNotice.setStatus(SystemNoticeConstants.SYSTEM_NOTICE_STATUS_READ);
            result.add(systemNotice);
        }
        systemNoticeService.updateBatchById(result);
        systemNoticeService.sendMessage();
        return ResponseEntity.ok(ResourceUtil.success());
    }
}
